#include "videowindow.h"
#include "ui_videowindow.h"

#include <QDir>
#include <QFileDialog>

VideoWindow::VideoWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::VideoWindow)
{
    ui->setupUi(this);

    ui->widget_video->hide();  //默认隐藏

    //创建对象
    this->player=new QMediaPlayer(this);  //创建播放器对象
    this->playlist=new QMediaPlaylist(this);  //创建播放列表对象

    //设置播放
    playlist->setPlaybackMode(QMediaPlaylist::Loop);  //循环模式
    player->setPlaylist(playlist);

    player->setNotifyInterval(2000);  //信息更新周期2000ms
    player->setVideoOutput(ui->widget_video); //视频显示
    ui->widget_video->setMediaPlayer(player);  //显示组件的关联播放器

    //绑定信号槽
    connect(player,SIGNAL(stateChanged(QMediaPlayer::State)),this,SLOT(onStateChanged(QMediaPlayer::State)));
    connect(player,SIGNAL(positionChanged(qint64)),this,SLOT(onPositionChanged(qint64)));
    connect(player,SIGNAL(durationChanged(qint64)),this,SLOT(onDurationChanged(qint64)));
}

VideoWindow::~VideoWindow()
{
    delete ui;
}

//播放状态槽函数
void VideoWindow::onStateChanged(QMediaPlayer::State state)
{
    if(state==QMediaPlayer::PlayingState)
    {
        ui->button_playState->setStyleSheet("border-image: url(:/resources/media/pause.png);");
    }
    else
    {
        ui->button_playState->setStyleSheet("border-image: url(:/resources/media/start.png);");
    }
}

//播放列表改变槽函数
void VideoWindow::onPlaylistChanged(int position)
{
    ui->listWidget->setCurrentRow(position);  //定位当前选中的行
    QListWidgetItem *item=ui->listWidget->currentItem();  //得到当前行的item

    if(item) ui->label_musicName->setText(item->text());  //显示item的名字
}

//总时长槽函数
void VideoWindow::onDurationChanged(qint64 duration)
{
    ui->horizontalSlider->setMaximum(duration);  //用当前文件的总时长设置进度条的最大值

    int sec=duration/1000;  //秒
    int min=sec/60;  //分
    sec=sec%60;  //余数秒

    durationTime=QString::asprintf("%d:%d",min,sec);
    ui->label_musicTime->setText(positionTime+"/"+durationTime);
}

//当前时长槽函数
void VideoWindow::onPositionChanged(qint64 position)
{
    if(ui->horizontalSlider->isSliderDown())
    {

        player->setPosition(ui->horizontalSlider->value());
    }

    ui->horizontalSlider->setSliderPosition(position);  //用position设置进度条当前位置

    int sec=position/1000;  //秒
    int min=sec/60;  //分
    sec=sec%60;  //余数秒

    positionTime=QString::asprintf("%d:%d",min,sec);
    ui->label_musicTime->setText(positionTime+"/"+durationTime);
}

//返回主页面按钮槽函数
void VideoWindow::on_button_back_clicked()
{
    this->hide();
    this->parentWidget()->show();
}

//添加视频按钮槽函数
void VideoWindow::on_button_add_clicked()
{
    QString curPath=QDir::homePath();  //获取用户目录
    QString dlgTitle="选择视频文件";  //窗口标题
    QString filter="视频文件(*.mp4 *.avi *.mkv);;mp4文件(*.mp4);;avi文件(*.avi);;mkv文件(*.mkv)";  //可选择文件
    QStringList fileList=QFileDialog::getOpenFileNames(this,dlgTitle,curPath,filter);

    if(fileList.count()<1) return;
    for(int i=0;i<fileList.count();i++)
    {
        QString aFile=fileList.at(i);  //获取第i个文件名
        playlist->addMedia(QUrl::fromLocalFile(aFile));  //添加到播放列表

        QFileInfo fileInfo(aFile);
        QString fileName=fileInfo.fileName().split(".")[0];  //获取不带后缀的文件名

        ui->listWidget->addItem(fileName);  //添加到显示列表
    }

    //添加完成后默认不播放
}

//删除视频按钮槽函数
void VideoWindow::on_button_delete_clicked()
{
    int pos=ui->listWidget->currentRow();  //获取当前选择的行数

    QListWidgetItem *item=ui->listWidget->takeItem(pos);  //根据行数获取item

    delete item;  //从显示列表删除
    playlist->removeMedia(pos);  //从播放列表删除
}

//上一个按钮槽函数
void VideoWindow::on_button_previous_clicked()
{
    int size=ui->listWidget->count();
    int row=ui->listWidget->currentRow();

    if(row==0)  //第一个的上一个是最后一个
    {
        ui->listWidget->setCurrentRow(size-1);
        playlist->setCurrentIndex(size-1);
    }
    else
    {
        row--;
        ui->listWidget->setCurrentRow(row);
        playlist->setCurrentIndex(row);
    }
}

//开始/暂停按钮槽函数
void VideoWindow::on_button_playState_clicked()
{
    if(player->state()==QMediaPlayer::PlayingState)  //播放状态点击按钮就暂停
    {
        player->pause();
    }
    else if(player->state()==QMediaPlayer::PausedState)  //暂停状态点击按钮就播发
    {
        if(playlist->currentIndex()<0) playlist->setCurrentIndex(0);  //未选择行默认播放第一首
        player->play();
    }
}

//下一个按钮槽函数
void VideoWindow::on_button_next_clicked()
{
    int size=ui->listWidget->count();
    int row=ui->listWidget->currentRow();

    if(row==size-1)  //最后一个的下一个是第一个
    {
        ui->listWidget->setCurrentRow(0);
        playlist->setCurrentIndex(0);
    }
    else
    {
        row++;
        ui->listWidget->setCurrentRow(row);
        playlist->setCurrentIndex(row);
    }
}

//全屏按钮槽函数
void VideoWindow::on_button_screen_clicked()
{
    ui->widget_video->setFullScreen(true);
}

//双击列表播放槽函数
void VideoWindow::on_listWidget_doubleClicked(const QModelIndex &index)
{
    ui->widget_video->show();

    int rowNo=index.row();
    playlist->setCurrentIndex(rowNo);
    player->play();
}

